using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
using Animator;

namespace TurbaconPhone
{

    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        CharDef redGuyDef, blueGuyDef;
        Character redGuy, blueGuy;

        public GameComponentCollection NewObjects, DeadObjects;
        public Game1()
        {
            NewObjects = new GameComponentCollection();
            DeadObjects = new GameComponentCollection();
            graphics = new GraphicsDeviceManager(this);
            Program.Game = this;
            Content.RootDirectory = "Content";
            // Frame rate is 30 fps by default for Windows Phone.
            TargetElapsedTime = TimeSpan.FromTicks(333333);
        }
        public void AddObject(GameComponent comp)
        {
            NewObjects.Add(comp);
        }
        public void RemoveObject(GameComponent comp)
        {
            DeadObjects.Add(comp);
        }
        protected override void Initialize()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            Graphics.Batch = spriteBatch;
            //Graphics.debugTexture = Content.Load<Texture2D>("Texture2Ds\\slice");
            TurBacon.init(this, spriteBatch);
            Physics.Initialize();
            Graphics.Initialize(graphics);
            base.Initialize();
        }

        protected override void LoadContent()
        {
            
            
            Character.LoadTextures(); 
          //  TurBacon.StateManager.Push(new GameplayState());
            TurBacon.StateManager.Push(new MenuState()); 
            Character.LoadTextures();
            
        }

        protected override void UnloadContent()
        {
        }

        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            //update objects
            foreach (GameComponent comp in NewObjects)
                Components.Add(comp);
            foreach (GameComponent comp in DeadObjects)
            {
                GameObject obj = (GameObject)comp;
                if (obj != null)
                    obj.Destroy();
                Components.Remove(comp);
            }
            //clear collections
            NewObjects.Clear();
            DeadObjects.Clear();
            //advance physics
            //Physics.Advance((float)gameTime.ElapsedGameTime.TotalSeconds);

            TurBacon.StateManager.Top().HandleInput();
            TurBacon.StateManager.Top().Update(gameTime);
            TurBacon.Input.Update();

            //redGuy.Update(gameTime);
            //blueGuy.Update(gameTime);
            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            Stack<State>.Enumerator Enumer = TurBacon.StateManager.getStates();
            //if (Enumer.Current != null)
            //{
            //    Enumer.Current.Draw(spriteBatch);
            //}
            List<State> states = new List<State>();
            while (Enumer.MoveNext())
            {
                states.Add( Enumer.Current);
            }
            for (int i = 0; i < states.Count; i++)
            {
                states[states.Count - i - 1].Draw(spriteBatch);
            }
            
            //TurBacon.StateManager.Top().Draw(spriteBatch);
            
            base.Draw(gameTime);
        }
    }
}
